﻿Imports System.Xml
Imports System.IO

Public Class frmMeetingRoom
    Dim xmldoc As New XmlDocument
    Dim dataTable As DataTable = New DataTable("MeetingRoom")
    Dim dataRow As DataRow
    Dim filePath As String = ""

    Private Sub ButtonCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCreate.Click
        AddDataToTable()
        LoadGridView()
    End Sub

    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Filter = "Text Files (*.gp)|*.gp|All Files (*.*)|*.*"
        If filePath.Equals("") Then
            If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                Dim FileName As String = SaveFileDialog.FileName
                SaveFile(FileName)
            End If
        Else
            Try
                File.Delete(filePath)
                SaveFile(filePath)
            Catch ex As Exception
                MessageBox.Show("This file have used by another program.")
            End Try

        End If

    End Sub

    Private Sub frmMeetingRoom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateDataTable()
    End Sub

    'Load data for GridView
    Private Sub LoadGridView()
        DataGridView_MeetingRoom.DataSource = dataTable
    End Sub

    'Create table save data what customer input
    Private Sub CreateDataTable()
        Dim Name As DataColumn = New DataColumn("Name", Type.GetType("System.String"))
        Dim Description As DataColumn = New DataColumn("Description", Type.GetType("System.String"))
        Dim Organization As DataColumn = New DataColumn("Organization", Type.GetType("System.String"))
        Dim Rules As DataColumn = New DataColumn("Rules", Type.GetType("System.String"))
        Dim Status As DataColumn = New DataColumn("Status", Type.GetType("System.String"))
        Dim Severity As DataColumn = New DataColumn("Severity", Type.GetType("System.String"))
        Dim Member As DataColumn = New DataColumn("Member", Type.GetType("System.String"))
        dataTable.Columns.Add(Name)
        dataTable.Columns.Add(Description)
        dataTable.Columns.Add(Organization)
        dataTable.Columns.Add(Rules)
        dataTable.Columns.Add(Status)
        dataTable.Columns.Add(Severity)
        dataTable.Columns.Add(Member)
    End Sub

    'Add data enter from textbox to table
    Private Sub AddDataToTable()
        dataRow = dataTable.NewRow()
        dataRow.Item("Name") = MyRichTextBox_Name.Text
        dataRow.Item("Description") = MyRichTextBox_Description.Text
        dataRow.Item("Organization") = MyRichTextBox_Organization.Text
        dataRow.Item("Rules") = MyRichTextBox_Rules.Text
        dataRow.Item("Status") = MyRichTextBox_Status.Text
        dataRow.Item("Severity") = MyRichTextBox_Severity.Text
        dataRow.Item("Member") = MyRichTextBox_Member.Text
        dataTable.Rows.Add(dataRow)
    End Sub

    'Save data from datatable to xml file with ext .qt
    Private Sub SaveFile(ByVal FileName As String)

        Try
            filePath = FileName
            ' Write node root for file meetingroom.qt
            Dim WriterQT As New StreamWriter(FileName, False, System.Text.Encoding.Unicode)
            WriterQT.WriteLine("<RootMeetingRoom>")
            WriterQT.WriteLine("</RootMeetingRoom>")
            WriterQT.Close()
            ' Write data to file
            xmldoc.Load(FileName)
            'nút gốc là Subject
            Dim inColumn As Integer = dataTable.Columns.Count()
            For Each rows As DataRow In dataTable.Rows
                Dim root As XmlElement = xmldoc.CreateElement("MeetingRoom")
                For i As Integer = 0 To inColumn - 1
                    Dim childName As XmlElement = xmldoc.CreateElement(dataTable.Columns.Item(i).ColumnName)
                    childName.AppendChild(xmldoc.CreateTextNode(rows.Item(i)))
                    root.AppendChild(childName)
                Next
                'thêm vào file xml
                xmldoc.DocumentElement.AppendChild(root)
            Next
                'lưu lại file xml
                xmldoc.Save(FileName)
                MessageBox.Show("Save success")
        Catch ex As Exception
            MessageBox.Show("Have error in process.Please try agian")
        End Try

    End Sub

    'Load data from file to Datatable
    Public Sub LoadXMLForDatable(ByVal FilePath As String)
        Try
            xmldoc.Load(FilePath)
            Dim nodeList As XmlNodeList = xmldoc.GetElementsByTagName("MeetingRoom")
            For Each node As XmlNode In nodeList
                Dim i As Integer = 0
                dataRow = dataTable.NewRow()
                For Each nodechild As XmlNode In node.ChildNodes
                    dataRow.Item(i) = nodechild.Value
                    i += 1
                Next
                dataTable.Rows.Add(dataRow)
            Next
            DataGridView_MeetingRoom.DataSource = dataTable
        Catch ex As Exception
            MessageBox.Show("Have a error in open file process.Please try again")
        End Try
    End Sub

    Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
        MyRichTextBox_Name.Text = ""
        MyRichTextBox_Description.Text = ""
        MyRichTextBox_Organization.Text = ""
        MyRichTextBox_Member.Text = ""
        MyRichTextBox_Rules.Text = ""
        MyRichTextBox_Severity.Text = ""
        MyRichTextBox_Status.Text = ""
    End Sub
End Class